package com.qpl.depression.cms.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qpl.depression.cms.common.ServerResponse;
import com.qpl.depression.cms.entity.Category;
import com.qpl.depression.cms.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 试题分类页面
     *
     * @return
     */
    @GetMapping("category/page")
    public String categoryPage() {
        return "categoryList";
    }

    /**
     * 试题分类数据
     *
     * @return
     */
    @GetMapping("category/data")
    @ResponseBody
    public ServerResponse categoryData(String name) {
        List<Category> categories = categoryService.list(Wrappers.<Category>lambdaQuery().like(StringUtils.isNotBlank(name), Category::getName, name).eq(Category::getStatus, 1));
        return ServerResponse.createBySuccess("success", categories);
    }

    /**
     * 试题分类添加页面
     *
     * @return
     */
    @GetMapping("category/add/page")
    public String categoryAddPage() {
        return "categoryAddPage";
    }

    /**
     * 试题分类保存
     *
     * @return
     */
    @PostMapping("category/save")
    @ResponseBody
    public ServerResponse categorySave(Category category) {
        return categoryService.saveOrUpdate(category) ? ServerResponse.createBySuccessMessage("成功") : ServerResponse.createByErrorMessage("失败");
    }

    /**
     * 试题分类编辑
     *
     * @return
     */
    @GetMapping("/category/edit")
    public String categoryEdit(Long id, ModelMap map) {
        Category category = categoryService.getById(id);
        map.addAttribute("category", category);
        return "categoryEditPage";
    }


}
